/**
 * @Title: SplashActivity.java
 * @Package com.unicom.eoms.activity
 * @Description: TODO  启动页
 * @author li_xiaoyue
 * @version 2013-4-2
 */
package com.hktf.moedokilock.activity;

import android.content.ComponentName;
import android.content.Intent;

import com.hktf.corelib.constant.CommonConstant;
import com.hktf.corelib.eventBus.LoginEvent;
import com.hktf.corelib.jsonbean.UserDto;
import com.hktf.corelib.jsonbean.response.LoginResult;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.LogMds;
import com.hktf.moedokilock.R;
import com.hktf.uilib.activity.ProtocolWebViewActivity;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.app.UiApplication;
import com.hktf.uilib.callback.BaseCallBackNew;
import com.hktf.uilib.engine.DriverEngine;
import com.hktf.uilib.utils.DlgUtils;
import com.hktf.uilib.utils.WxsdkUtil;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author cdz
 * @ClassName: LoginActivity
 * @Description: TODO 登录页
 * @date 2025-3-2 上午9:15:07
 */
public class LoginActivity extends BaseActivity {


    private android.widget.LinearLayout splashRl;
    private android.widget.ImageView ivSplashLogo;
    private android.widget.LinearLayout llLogin;
    private android.widget.FrameLayout flCb;
    private android.widget.CheckBox cb;
    private android.widget.TextView tvProtocol;
    private android.widget.TextView tvProtocol1;

    @Override
    public int getLayoutResID() {
        return R.layout.act_login;
    }




    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void beforeAddtoStack() {
        EventBus.getDefault().post(new LoginEvent(LoginEvent.LOGIN_OUT));
        CommUtils.userLogout();
        UiApplication.getInstance().getActivityManager().popAllActivities();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (flag==1) {
            flag=0;
            show1();
        }else if (flag==2) {
            flag=0;
            show2();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    protected void onDestroy() {
        super.onDestroy();

    }

    public void initView() {
        splashRl = findViewById(R.id.splash_rl);
        ivSplashLogo = findViewById(R.id.iv_splash_logo);
        llLogin = findViewById(R.id.ll_login);
        flCb = findViewById(R.id.fl_cb);
        cb = findViewById(R.id.cb);
        tvProtocol = findViewById(R.id.tv_protocol);
        tvProtocol1 = findViewById(R.id.tv_protocol1);

        flCb.setOnClickListener(v -> {
            cb.setChecked(!cb.isChecked());
        });
        tvProtocol.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, ProtocolWebViewActivity.class);
            intent.putExtra("flag", 1);
            startActivity(intent);
        });
        tvProtocol1.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, ProtocolWebViewActivity.class);
            intent.putExtra("flag", 0);
            startActivity(intent);
        });
        llLogin.setOnClickListener(v -> {
            if (!cb.isChecked()) {
                show1();
                return;
            }
            WxsdkUtil.gotoWxlogin();
        });
    }
    private int flag;

    private void show1(){
        DlgUtils.showProtocol(this, new DlgUtils.PickListener() {
            @Override
            public void onPick(int index) {
                Intent intent = new Intent(LoginActivity.this, ProtocolWebViewActivity.class);
                switch (index) {
                    case 1:
                        intent.putExtra("flag", 1);
                        startActivity(intent);
                        flag=1;
                        break;
                    case 2:
                        intent.putExtra("flag", 0);
                        startActivity(intent);
                        flag=1;
                        break;
                    case 3:
                        show2();
                        break;
                    case 4:
                        cb.setChecked(true);
                        WxsdkUtil.gotoWxlogin();
                        break;
                }
            }

            @Override
            public void onCancel() {

            }
        });
    }
    private void show2(){
        DlgUtils.showProtocol2(this, new DlgUtils.PickListener() {
            @Override
            public void onPick(int index) {
                Intent intent = new Intent(LoginActivity.this, ProtocolWebViewActivity.class);
                switch (index) {
                    case 1:
                        intent.putExtra("flag", 1);
                        startActivity(intent);
                        flag=2;
                        break;
                    case 2:
                        intent.putExtra("flag", 0);
                        startActivity(intent);
                        flag=2;
                        break;
                    case 3:
                        finish();
                        break;
                    case 4:
                        cb.setChecked(true);
                        WxsdkUtil.gotoWxlogin();
                        break;
                }
            }

            @Override
            public void onCancel() {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvent event) {
        LogMds.w("cdz", "收到消息了............login=");
        if (event != null) {
            int event_id = event.getEvent_id();
            switch (event_id) {
                case LoginEvent.LOGIN_SUCCESS:
                    break;
                case LoginEvent.GET_CODE_SUCCESS:
                    showLoadingDlg(getString(com.hktf.uilib.R.string.login_loading));
                    DriverEngine.wxLogin(event.getCode(),new BaseCallBackNew<LoginResult>(LoginResult.class){
                        @Override
                        public void onSUCCESS(Response<LoginResult> response) {
                            LoginResult body = response.body();
                            UserDto userDto = body.getData();
                            CommUtils.saveUserInfo(userDto);

//                            EventBus.getDefault().post(new LoginEvent(LoginEvent.LOGIN_SUCCESS, userDto, null));
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                    break;
            }
        }
    }
}
